<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 * Description of JinLi
 *
 * @author kolin add 2014.08.29  modify by dragon.hu @2016.2.3
 */
require_once 'BaseChannel.php';

//金立支付;
class JinLi implements BaseChannel {

    //构造函数;
    function __construct() {
        
    }

    function rsa_verify($post_arr) {
        ksort($post_arr);
        foreach ($post_arr as $key => $value) {
            if ($key == 'sign')
                continue;
            $signature_str .= $key . '=' . $value . '&';
        }
        $signature_str = substr($signature_str, 0, -1);
        $publickey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCJdm62U/hJlNDVL7JD0bvj9MlxTiXD+5ngd45Vi/MXtbYZ5NRS53hS9Sca88Mou0gVlFO2VM/A+LkwHn+O5qt+7EEt/LlxDfae4OJYvCG4BtYjF294g1dbruAFi1lbOktIZgZrYhAc/eo8GswvrD1qfm1iwyQQjIXZPMjwxbxsewIDAQAB';
        $pem = chunk_split($publickey, 64, "\n");
        $pem = "-----BEGIN PUBLIC KEY-----\n" . $pem . "-----END PUBLIC KEY-----\n";
        $public_key_id = openssl_pkey_get_public($pem);
        $signature = base64_decode($post_arr['sign']);
        return openssl_verify($signature_str, $signature, $public_key_id);
    }

//安卓支付验证;
    function android($data) {
        $paymentKey = " "; //支付密钥
        $ret = $this->paymentCallBack($paymentKey, ANDROID);
        echo $ret;
    }

    ///IOS支付验证;
    function ios($data) {
        $paymentKey = "6"; //支付密钥
        $ret = $this->paymentCallBack($paymentKey, IOS);
        echo $ret;
    }

    ///验证;
    function paymentCallBack($paymentKey, $platform) {
        $orderLog = new ChargeOrder();
        $orderLog->log("jinli", $_REQUEST);

        $apikey = $_REQUEST["api_key"];
        $closetime = $_REQUEST["close_time"];
        $createtime = $_REQUEST["create_time"];
        $dealprice = $_REQUEST["deal_price"];
        $out_order_no = $_REQUEST["out_order_no"];
        $paychannel = $_REQUEST["pay_channel"];
        $submittime = $_REQUEST["submit_time"];
        $userid = $_REQUEST["user_id"];
        $sign = $_REQUEST["sign"];
        
        if(!$this->rsa_verify($_REQUEST)){
            ///验证失败;
            ///die('error sign');
			$orderLog->log("jinli","sign verify fail!");
			$ret = "Failed1";
        }
    else {
            ///验证通过;
            if(false == $orderLog->recordOrder(STATUS_SUCCESS, $dealprice,$out_order_no, "jinli", $platform)) {
                $ret = "Failed2";
                $orderLog->log("jinli",array("order"=>$out_order_no,"money"=>$dealprice,"result"=>$result,"serverresult"=>"faild"));
            } else {
                $ret = "success";
                $orderLog->log("jinli",array("order"=>$out_order_no,"money"=>$dealprice,"result"=>$result,"serverresult"=>"success"));
            }
        } 
        return $ret;
    }

}

?>
